import carInfo from '@/components/acessory/carInfo.vue'
import defaultImg from '../../components/defaultImg/defaultImg'
export default {
  components: {
    carInfo,
    defaultImg,
  },

  data() {
    return {
      characterList: ['品质不限', '原厂', '品牌', '拆车件', '其它'],
      carInfo: {}, //车辆信息
      id: '',
      carPartDetails: {}, //配件信息
      keyword: '', //搜索关键字
      oldKeyword: '', //上一次的关键字
      characterType: 0, //品质 :0=不限，1=原厂，2=品牌，3=拆车件，4=其他
      sort: 0, //排序 0=综合排序，1=价格从低到高，2=由近到远(lat、lon 必传)
      supplier: 0, //供应商
      sortList: [
        {
          value: 0,
          label: '综合排序',
        },
        {
          value: 1,
          label: '价格从低到高',
        },
        {
          value: 2,
          label: '距离由近到远',
        },
      ],
      characterTypeList: [
        {
          value: 0,
          label: '全部',
        },
        {
          value: 1,
          label: '原厂',
        },
        {
          value: 2,
          label: '品牌',
        },
        {
          value: 3,
          label: '拆车件',
        },
        {
          value: 4,
          label: '其它',
        },
      ],
      supplierList: [
        {
          value: 0,
          label: '敬请期待',
        },
      ],
      current: 0, // 预设当前项的值
      timer: null, // 定时器
      shifouhuad: false,
      TotalPayMoney: 0, //总价
      isSticky: false,
      isOpen: false,
      selQuoteArr: [],
      selNum: 0,
      car_Part_Offer_Details_Ids: [], //提交接口所需数据
    }
  },

  watch: {
    sort() {
      this.getInfo()
    },
    characterType() {
      this.getInfo()
    },
  },

  onLoad(e) {
    this.id = e.id
    this.getInfo()
  },

  methods: {
    //获取报价信息
    getInfo() {
      this.$api.accessory
        .getQupoteInfo({
          id: this.id,
          keyWords: this.keyword,
          character_Type: this.characterType,
          lat: this.$store.state.latitude
            ? this.$store.state.latitude
            : 104.063364,
          lon: this.$store.state.longitude
            ? this.$store.state.longitude
            : 30.566,
          sort: this.sort,
        })
        .then((res) => {
          this.carPartDetails = res.data.carPartDetails
          this.carInfo = {
            inquiryId: this.id,
            bid: res.data.brand_Id,
            brand: res.data.brand_Name,
            brand_Logo: '/goodimg/brandimg/' + res.data.brand_Id + '.png',
            cartypeid: res.data.car_Type_Id,
            cartypename: res.data.car_Type_Name,
            id: res.data.configuration_Id,
            key: `${res.data.brand_Id} ${res.data.car_Type_Id}${res.data.configuration_Id}`,
            name: res.data.configuration_Name,
            vin: res.data.vin_No,
          }
          for (let i = 0; i < this.carPartDetails.length; i++) {
            let tmp = []
            for (
              let j = 0;
              j < this.carPartDetails[i].carPartOfferList.length;
              j++
            ) {
              tmp.push({
                id: this.carPartDetails[i].carPartOfferList[j].id,
                price: this.carPartDetails[i].carPartOfferList[j].offer_Price,
                number: this.carPartDetails[i].carPartOfferList[j]
                  .purchase_Number,
                isChecked: false,
              })
            }
            this.selQuoteArr.push(tmp)
          }
        })
    },

    //搜索
    search() {
      if (this.oldKeyword !== this.keyword) {
        this.oldKeyword = this.keyword
        this.getInfo()
      }
    },

    sticky(type) {
      this.isSticky = type
    },

    open() {
      this.isOpen = true
      uni.pageScrollTo({
        scrollTop: 268,
        duration: 100,
      })
    },

    close() {
      this.isOpen = false
    },

    selectedclick(index, index1) {
      if (this.selQuoteArr[index][index1].is_Purchase) {
        return
      }
      for (let i = 0; i < this.selQuoteArr[index].length; i++) {
        if (i === index1) {
          this.selQuoteArr[index][i].isChecked = !this.selQuoteArr[index][i]
            .isChecked
        } else {
          this.selQuoteArr[index][i].isChecked = false
        }
      }
      this.plusTotalPayMoeny()
    },

    //计算总支付金额
    plusTotalPayMoeny() {
      let totalpaymoney = 0
      this.selNum = 0
      this.car_Part_Offer_Details_Ids = []
      for (let i = 0; i < this.selQuoteArr.length; i++) {
        const tmp = []
        for (let j = 0; j < this.selQuoteArr[i].length; j++) {
          if (this.selQuoteArr[i][j].isChecked) {
            totalpaymoney +=
              this.selQuoteArr[i][j].price * this.selQuoteArr[i][j].number
            this.selNum++
            this.car_Part_Offer_Details_Ids.push(this.selQuoteArr[i][j].id)
          }
        }
      }
      this.TotalPayMoney = totalpaymoney.toFixed(2)
    },

    //确认
    confirm() {
      if (this.car_Part_Offer_Details_Ids.length === 0) {
        uni.showToast({
          title: '请选择需购买的配件',
          icon: 'none',
          duration: 3000,
        })
        return
      }
      uni.navigateTo({
        url:
          '/pages_findAccessory/submitOrder/submitOrder?type=1&data=' +
          JSON.stringify(this.car_Part_Offer_Details_Ids),
      })
    },

    chat(data) {
      const toUserInfo = {
        user: data.store_Name, //店铺名称
        img: data.store_Topimg, //店铺头像
        lastTime: '', //
        userId: data.store_Id, //店铺id
        conversationID: 'C2C' + data.store_Id,
      }
      let carPartDetailsStr = ''
      for (let i = 0; i < this.carPartDetails.length; i++) {
        if (i > 4) {
          break
        }
        carPartDetailsStr =
          carPartDetailsStr + this.carPartDetails[i].part_Category_Name + ' '
      }
      this.$store.commit('news/createConversationActive', toUserInfo)
      this.$store.commit('news/setCardInfo', {
        imgSrc:
          'https://files.yzsheng.com/goodimg/brandimg/' +
          this.carInfo.bid +
          '.png',
        name:
          this.carInfo.brand +
          ' ' +
          this.carInfo.cartypename +
          ' ' +
          this.carInfo.name,
        carPartDetailsStr: carPartDetailsStr,
        type: 'accessory',
      })
      uni.navigateTo({
        url:
          '/pages_news/contactSeller/contactSeller?user=' +
          data.store_Name +
          '&pageId=' +
          this.id +
          '&tab=0',
      })
    },

    call(phone) {
      uni.makePhoneCall({
        phoneNumber: phone,
      })
    },

    // 点击左边的栏目切换
    async swichMenu(item, index) {
      // this.bname = item.name;
      // if (this.arr.length == 0) {
      // 	await this.getMenuItemTop();
      // }
      if (index == this.current) return
      this.$nextTick(function () {
        // this.scrollRightTop = this.arr[index];
        this.current = index
        // this.leftMenuStatus(index);
      })
    },

    // 获取一个目标元素的高度
    getElRect(elClass, dataVal) {
      new Promise((resolve, reject) => {
        const query = uni.createSelectorQuery().in(this)
        query
          .select('.' + elClass)
          .fields(
            {
              size: true,
            },
            (res) => {
              // 如果节点尚未生成，res值为null，循环调用执行
              if (!res) {
                setTimeout(() => {
                  this.getElRect(elClass)
                }, 10)
                return
              }
              this[dataVal] = res.height
              resolve()
            }
          )
          .exec()
      })
    },

    // 设置左边菜单的滚动状态
    async leftMenuStatus(index) {
      this.bname = this.tabbar[index].name
      this.current = index
      // 如果为0，意味着尚未初始化
      if (this.menuHeight == 0 || this.menuItemHeight == 0) {
        await this.getElRect('menu-scroll-view', 'menuHeight')
        await this.getElRect('u-tab-item', 'menuItemHeight')
      }
      // 将菜单活动item垂直居中
      // this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
    },
  },
}
